package com.xingkeduo.business.user.service;

import com.xingkeduo.dao.database.DatabaseAccessObject;
import com.xingkeduo.pojo.query.JobseekerJobDetailQO;
import com.xingkeduo.pojo.table.JobseekerJobDetailTO;
import com.xingkeduo.pojo.table.JobseekerJobTO;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.IdentityUtil;
import com.xingkeduo.util.tool.IdentityUtil.Worker;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * 求职者封装类
 * 
 * @author 高杰
 * @time 2018年12月25日 下午1:41:24
 * @description
 */
@Component
public class JobseekerJobDetailService {
	private static final String NAMESPACE = CommonUtil.formatNamespace(JobseekerJobDetailTO.class);
	@Resource(name = "mysqlUserMasterDao")
	private DatabaseAccessObject<JobseekerJobDetailTO, JobseekerJobDetailQO> dao;

	/**
	 * 查询
	 * 
	 * @author 高杰
	 * @time 2018年12月27日 下午12:04:12
	 * @param code
	 * @return
	 */
	public List<JobseekerJobDetailTO> selectByJobseekerId(long jobseekerId) throws Throwable {
		JobseekerJobDetailQO jobseekerJobDetailQuery = new JobseekerJobDetailQO().setJobseekerId(jobseekerId).setOrderBy("starttime DESC").setStartTime("1").setIsDelete(YesNoEnum.NO);
		return dao.select(NAMESPACE, jobseekerJobDetailQuery);
	}

	/**
	 * 新增
	 * 
	 * @author 高杰
	 * @time 2018年12月25日 下午2:19:27
	 * @param jobseeker
	 * @throws Throwable
	 */
	@Transactional
	public void insert(JobseekerJobDetailTO jobseekerJobDetail) throws Throwable {
		jobseekerJobDetail.setId(IdentityUtil.nextId(Worker.USER));
		if (dao.insert(NAMESPACE, jobseekerJobDetail) < 1)
			throw new DataException(DataException.Type.FAIL, "求职者工作经历");
	}

	/**
	 * 新增
	 * 
	 * @author 高杰
	 * @time 2018年12月25日 下午2:19:27
	 * @param jobseeker
	 * @throws Throwable
	 */
	@Transactional
	public void insert(List<JobseekerJobDetailTO> jobseekerJobDetailList, JobseekerJobTO jobseekerJob)
			throws Throwable {
		for (Iterator<JobseekerJobDetailTO> iterator = jobseekerJobDetailList.iterator(); iterator.hasNext();) {
			iterator.next().initId(IdentityUtil.nextId(Worker.USER)).setJobseekerId(jobseekerJob.getId())
					.setCreatedBy(jobseekerJob.getId()).setUpdatedBy(jobseekerJob.getId());
		}
		if (dao.insert(NAMESPACE, jobseekerJobDetailList) < jobseekerJobDetailList.size())
			throw new DataException(DataException.Type.FAIL, "求职者工作经历");
	}

	/**
	 * 更新
	 * 
	 * @author 高杰
	 * @time 2018年12月25日 下午2:19:27
	 * @param jobseeker
	 * @throws Throwable
	 */
	@Transactional
	public int update(JobseekerJobDetailTO jobseekerJobDetail) throws Throwable {
		return dao.update(NAMESPACE, jobseekerJobDetail);
	}


	@Transactional
	public int updateList(List<JobseekerJobDetailTO> list) throws Throwable {
		int count = 0;
		for (Iterator<JobseekerJobDetailTO> iterator = list.iterator(); iterator.hasNext(); ) {
			count += dao.update(NAMESPACE, iterator.next());
		}
		return count;
	}

}
